package com.homedo.odin.solution.controller.mng;

import com.homedo.core.common.base.controller.BaseController;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.NumberHelper;
import com.homedo.odin.solution.enumcode.basic.ShelfEnum;
import com.homedo.odin.solution.model.entity.AdPosition;
import com.homedo.odin.solution.model.request.mng.adservings.MngAdServingsListRequestBean;
import com.homedo.odin.solution.model.request.mng.adservings.MngAdServingsRequestBean;
import com.homedo.odin.solution.model.response.mng.adservings.MngAdServingsPageResponseBean;
import com.homedo.odin.solution.service.AdServingServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import javax.validation.Valid;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: zhanglei
 * @date: 2018/5/15
 * @Description:
 */
@Api(description = "广告", tags = {"AdServings"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/mng/ad_servings")
public class MngAdServingsController extends BaseController<AdServingServiceImpl> {

    @ApiOperation(value = "MNG_发布新广告", httpMethod = "POST")
    @PostMapping
    public ResultData addAdvertising(@Valid @RequestBody MngAdServingsRequestBean requestBean,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }
        baseService.addAdvertising(requestBean);
        return ResultData.newInstance(Meta.CODE_200, null);
    }

    @ApiOperation(value = "MNG_获取广告列表", response = MngAdServingsPageResponseBean.class, httpMethod = "GET")
    @GetMapping
    public ResultData getAdvertising(@Valid @ModelAttribute MngAdServingsListRequestBean requestBean,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }
        MngAdServingsPageResponseBean responseBean = baseService.getAdvertising(requestBean);
        return ResultData.newInstance(Meta.CODE_200, responseBean);
    }

    @ApiOperation(value = "MNG_广告上下架", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Long", name = "userId", value = "操作人ID", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "String", name = "adNumber", value = "广告编号", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "status", value = "上下架状态：0：已下架，1：上架中", required = true)})
    @PutMapping("/{adNumber}/{status}")
    public ResultData shelfStatus(@RequestParam Long userId, @PathVariable String adNumber,
            @PathVariable Integer status) {
        if (!NumberHelper.validate(userId)) {
            throw new BizException(Meta.CODE_451.getCode(), "用户Id非法");
        }

        if (!ShelfEnum.dbValueExist(status)) {
            throw new BizException(Meta.CODE_451.getCode(), "上下架参数非法");
        }

        baseService.shelfStatus(userId, adNumber, status);
        return ResultData.newInstance(Meta.CODE_200, null);
    }


    @ApiOperation(value = "MNG_所有广告位", response = AdPosition.class, httpMethod = "GET")
    @GetMapping("/positions")
    public ResultData getAllPositions() {
        return ResultData.newInstance(Meta.CODE_200, baseService.selectAllPositions());
    }


}
